﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using WCYCReader.Main;
using WCYCReader.Models;
using WCYCReader.ShareFolder.ViewModel;
using WCYCReader.StorageHelper;
using WCYCReader.Utility;

namespace WCYCReader.Pages
{
    public partial class BookIntoPage : PhoneApplicationPage
    {


        public BookIntoPage()
        {
            InitializeComponent();
            this.Loaded += BookIntoPage_Loaded;
        }

        void BookIntoPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (App.ViewModel.IsDataLoaded)
            {
                base.DataContext = null;
            }
            else
            {
                App.ViewModel.LoadData();
                App.ViewModel.LoadSettings();
            }
            App.ViewModel.SelectedConnection = null;
            base.DataContext = App.ViewModel;
            //IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication();
            //if (!userStoreForApplication.DirectoryExists("content"))
            //{
            //    userStoreForApplication.CreateDirectory("content");
            //}
            IsolatedStorageHelper.Instance.CreateDirectory("content");
        }





        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            NaviService.Navigate(QQReaderPage.EPCAddressPage);

        }

        private void miEditAddress_Click(object sender, RoutedEventArgs e)
        {
            ConnectionViewModel model = GetConnectionViewModelByMenuItem(sender);
            if (model == null)
            {
                Prompt.PromptMessage("选择失效，请重试！");
                return;
            }

            App.ViewModel.SelectedConnection = model;
            NaviService.Navigate(QQReaderPage.EPCAddressPage);
        }

        private void miDeleteAddress_Click(object sender, RoutedEventArgs e)
        {
            ConnectionViewModel model = GetConnectionViewModelByMenuItem(sender);
            if (model == null)
            {
                Prompt.PromptMessage("选择失效，请重试！");
                return;
            }


        }

        private ConnectionViewModel GetConnectionViewModelByMenuItem(object sender)
        {
            MenuItem mi = sender as MenuItem;
            if (mi == null)
                return null;
            ConnectionViewModel model = mi.DataContext as ConnectionViewModel;
            if (model == null)
                return null;

            return model;
        }

        #region 进入电脑设备
        private void btnViewFiles_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            Button btn = sender as Button;
            if (btn == null)
                return;
            ConnectionViewModel model = btn.DataContext as ConnectionViewModel;
            if (model == null)
                return;
            App.ViewModel.SelectedConnection = model;
            NaviService.Navigate(QQReaderPage.EExplorerPage);

        }
        private void lstAddress_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.lstAddress.SelectedItem != null)
            {
                App.ViewModel.SelectedConnection = this.lstAddress.SelectedItem as ConnectionViewModel;
                NaviService.Navigate(QQReaderPage.EExplorerPage);
            }
        }

        private void miConnectAddress_Click(object sender, RoutedEventArgs e)
        {
            MenuItem mi = sender as MenuItem;
            if (mi == null)
                return;
            ConnectionViewModel model = mi.DataContext as ConnectionViewModel;
            if (model == null)
                return;
            App.ViewModel.SelectedConnection = model;
            NaviService.Navigate(QQReaderPage.EExplorerPage);
        }
        #endregion 进入电脑设备
    }
}